let indexModel = require('../model/indexModel');
let moment = require('moment');
module.exports = {
    showIndexPage: (req, res) => {
        let pagesize = 4;
        let nowPage = req.query.page || 1;
        indexModel.getArticles(pagesize, nowPage, (err, data) => {
            // 在返回data之前，需要将data中的ctime格式化一下
            data[0].forEach(item => {
                item.ctime = moment(item.ctime).fromNow();
            });
            // 计算总页码数，并将总页码数返回给前台
            let totalPage = Math.ceil(data[1][0].totalCount / pagesize);
            res.render('index', {
                isLogin: req.session.isLogin,
                user: req.session.user,
                list: data[0],
                totalPage: totalPage,
                nowPage: parseInt(nowPage)
            });
        });
    }
}